#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
__author__ = "Administrator"
__name__="mtfacfib"
__date__="2016/9/19"
__time__="14:17"
"""
'''
单线程中，只要逐个调用这些函数，在函数结束后，显示对应的结果
多线程中，会等到结束时才会调用getResult()函数，并在最后显示每个函数的结果
'''
from myThread import MyThread
from time import sleep,ctime

def fib(x):
    sleep(0.005)
    if x<2:
        return 1
    return (fib(x-2) + fib(x-1))

def fac(x):
    sleep(0.1)
    if x < 2:return 1
    return (x * fac(x-1))
def sum(x):
    sleep(0.1)
    if x < 2:return 1
    return (x + sum(x-1))

funcs = (fib,fac,sum)
n =12

def main():
    nfuncs = range(len(funcs))
    print '*** single thread ***'
    for i in nfuncs:
        print 'starting',funcs[i].__name__,'at:',ctime()
        print funcs[i](n)
        print funcs[i].__name__,'finished at:',ctime()

    print '\n*** multiple threads ***'
    thrads = []
    for i in nfuncs:
        t = MyThread(funcs[i],(n,),funcs[i].__name__)
        thrads.append(t)

    for i in nfuncs:
        thrads[i].start()

    for i in nfuncs:
        thrads[i].join()
        print thrads[i].getResult()

    print 'all DONE !!!!'

if __name__ == '__main__':
    main()
    print fib.__name__
















